package com.woniu.cinema.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.woniu.cinema.domain.MovieImage;
import com.woniu.cinema.mapper.MovieImageMapper;
import com.woniu.cinema.request.DynamicMovieRequest;
import com.woniu.cinema.vo.MovieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.woniu.cinema.mapper.MovieMapper;
import com.woniu.cinema.domain.Movie;
import com.woniu.cinema.service.MovieService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 影片信息Service业务层处理
 * 
 * @author yy
 * @date 2021-04-16
 */
@Service
public class MovieServiceImpl implements MovieService
{
    @Autowired
    private MovieMapper movieMapper;

    @Autowired
    private MovieImageMapper movieImageMapper;


    /**
     * 查询影片信息
     * 
     * @param movieId 影片信息ID
     * @return 影片信息
     */
    @Override
    public Movie selectMovieById(Long movieId)
    {
        Movie movie1 = movieMapper.selectMovieById(movieId);
        List<MovieImage> movieImageList = movieImageMapper.selectMovieImageListByMovieId(movie1.getMovieId());
        //使用stream把MovieImage列表转化成String的列表
        List<String> imageUrlList = movieImageList.stream().map(s -> s.getMovieImg()).collect(Collectors.toList());
        //把图片地址用逗号拼接成一个字符串
        String imgUrls = CollUtil.join(imageUrlList, ",");
        movie1.setMovieImg(imgUrls);
        return movie1;

    }

    /**
     * 查询影片信息列表
     * 
     * @param movie 影片信息
     * @return 影片信息
     */
    @Override
    public List<Movie> selectMovieList(Movie movie)
    {
        //循环列表，对于每一个电影，把movieImg字段的值替换为多个图片地址，并用逗号隔开
        List<Movie> movieList = movieMapper.selectMovieList(movie);

        return movieList;


    }

    /**
     * 新增影片信息
     * 
     * @param movie 影片信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMovie(Movie movie)
    {

        String movieImg = movie.getMovieImg();

        //1618567029616_.jpg,1618567032866_.png  把movieImg里面的第一个图片名称截取出来，放在movie的movieImg字段中
        if(movieImg!=null){
            movie.setMovieImg(movieImg.substring(0,movieImg.indexOf(",")));
        }

        //插入movie一条记录tb_movie_image
        movieMapper.insertMovie(movie);

        Long movieId = movie.getMovieId();

        //把所有的图片地址和movieId插入
        if (movieImg != null) {
            String[] imgUrl = movieImg.split(",");
            for (int i = 0; i < imgUrl.length; i++) {
                MovieImage movieImage = new MovieImage();
                movieImage.setMovieId(movieId);
                movieImage.setMovieImg(imgUrl[i]);
                if(i==0){
                    movieImage.setType(1L);
                }else if(i==1){
                    movieImage.setType(2L);
                }else{
                    movieImage.setType(3L);
                }
                movieImageMapper.insertMovieImage(movieImage);
            }
        }
        return 1;
    }

    /**
     * 修改影片信息
     * 
     * @param movie 影片信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateMovie(Movie movie)
    {
        //删掉图片列表
        movieImageMapper.deleteMovieImageByMovieId(movie.getMovieId());
        String movieImg = movie.getMovieImg();
        //1618567029616_.jpg,1618567032866_.png  把movieImg里面的第一个图片名称截取出来，放在movie的movieImg字段中
        if(movieImg!=null){
            movie.setMovieImg(movieImg.substring(0,movieImg.indexOf(",")));
        }
        movieMapper.updateMovie(movie);
        //把所有的图片地址和movieId插入
        if (movieImg != null) {
            String[] imgUrl = movieImg.split(",");
            for (int i = 0; i < imgUrl.length; i++) {
                MovieImage movieImage = new MovieImage();
                movieImage.setMovieId(movie.getMovieId());
                movieImage.setMovieImg(imgUrl[i]);
                if(i==0){
                    movieImage.setType(1L);
                }else if(i==1){
                    movieImage.setType(2L);
                }else{
                    movieImage.setType(3L);
                }
                movieImageMapper.insertMovieImage(movieImage);
            }
        }

        return 1;
    }

    /**
     * 批量删除影片信息
     * 
     * @param movieIds 需要删除的影片信息ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteMovieByIds(Long[] movieIds)
    {
        for (int i = 0; i < movieIds.length; i++) {
            movieMapper.deleteMovieById(movieIds[i]);
        }
        return movieMapper.deleteMovieByIds(movieIds);
    }

    /**
     * 删除影片信息信息
     * 
     * @param movieId 影片信息ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteMovieById(Long movieId)
    {
        movieImageMapper.deleteMovieImageByMovieId(movieId);
        movieMapper.deleteMovieById(movieId);
        return 1;
    }

    @Override
    public List<Movie> selectMovieListByDynamicFields(DynamicMovieRequest request) {

        return movieMapper.selectMovieListByDynamicFields(request);
    }

    @Override
    public List<Movie> listComingMovieListByCinema(Long cinemaId) {

        return movieMapper.listComingMovieListByCinema(cinemaId);

    }
    @Override
    public MovieVo selectMovieVoById(Long movieId){
        return movieMapper.selectMovieVoById(movieId);
    }

    @Override
    public List<MovieVo> listAll(Long id) {
        if(id<0){
            return movieMapper.selectMovieVoList();
        }else{
            return Arrays.asList(movieMapper.selectMovieVoById(id));
        }


    }
}
